/*
 * Copyright 2012 Nicholas Bilyk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package Util {

	public class NumberUtil {

		/**
		 * Pads a number with 0s.
		 * 
		 * @param n The number to pad.
		 * @param integerDigits The minimum number of integer digits to display.
		 * @param fractionDigits The minimum number of fraction digits to display. 
		 * 
		 * e.g. 
		 * padNumber(1.34, 2, 4) // 01.3400
		 * padNumber(1.34, 1, 0) // 1.34
		 * padNumber(1.34, 3, 0) // 001.34
		 * 
		 * 
		 * @author nbilyk
		 */
		public static function padNumber(n:Number, integerDigits:uint = 1, fractionDigits:uint = 0):String {
			var str:String = n.toString();
			var split:Vector.<String> = str.split(".");
			
			var intPart:String = split[0];
			var intDiff:int = integerDigits - intPart.length;
			while (intDiff > 0) {
				intPart = "0" + intPart;
				intDiff--;
			}
			
			var fracPart:String = split.length > 1 ? split[1] : "";
			var fracDiff:int = fractionDigits - fracPart.length;
			while (fracDiff > 0) {
				fracPart = fracPart + "0";
				fracDiff--;
			}
			
			if (fracPart.length > 0) return intPart + "." + fracPart;
			else return intPart;
		}
	}
}
